Skip to content

⚡️ Golf sqrt and cbrt by getting a better initial estimate and removing an iteration#1515

Merged
atarpara merged 2 commits into
Vectorized:clzfrom
duncancmt:dcmt/golf-roots
May 11, 2026
Merged

⚡️ Golf sqrt and cbrt by getting a better initial estimate and removing an iteration#1515
atarpara merged 2 commits into
Vectorized:clzfrom
duncancmt:dcmt/golf-roots

Conversation

@duncancmt

@duncancmt duncancmt commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Description

Both sqrt and cbrt can be gas optimized. sqrt only needs 6 Babylonian steps. A better initial estimate in cbrt allows us to shave a Newton-Raphson iteration later; also going down to 6.

Implementation by a meatbag. Formal proof of correctness is vibe coded.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

@atarpara atarpara merged commit 2bb67eb into Vectorized:clz May 11, 2026
atarpara pushed a commit that referenced this pull request May 13, 2026
…moving an iteration (#1515)

* ⚡️Golf `sqrt` and `cbrt` by getting a better initial estimate and removing an iteration

* ⚡️Golf `cbrt` a little bit more by cutting corners during initialization
atarpara added a commit that referenced this pull request May 13, 2026
* rebase with origin/main

* T

* Added test

* T

* T

* ⚡️ Golf `sqrt` and `cbrt` by getting a better initial estimate and removing an iteration (#1515)

* ⚡️Golf `sqrt` and `cbrt` by getting a better initial estimate and removing an iteration

* ⚡️Golf `cbrt` a little bit more by cutting corners during initialization

* ⚡️ Golf `cbrt` by using a small table for an even better initial estimate and removing another iteration (#1523)

* ⚡️ Golf `cbrt` by using a small table for an even better initial estimate and removing another iteration

* Formatting

* T

---------

Co-authored-by: duncancmt <1207590+duncancmt@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants